package com.yhy.client;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * describe:
 *
 * @author helloworldyu
 * @date 2018/5/8
 */
public class DatagramStringEncoder extends MessageToMessageEncoder<String> {
	private final InetSocketAddress remoteAddress;

	public DatagramStringEncoder(String host, int port) {
		this.remoteAddress = new InetSocketAddress(host,port);
	}

	@Override
	protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
		ByteBuf buffer = ctx.alloc().buffer();
		buffer.writeBytes(msg.getBytes());
		out.add(new DatagramPacket(buffer,remoteAddress));
	}
}